/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence.dao;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Project;
import model.ScheduleBaseline;
import org.apache.commons.logging.Log;
import persistence.PersistFactory;
import persistence.SysSql;
import persistence.interfaces.IProjectDao;
import persistence.services.BaseDao;
import persistence.services.PersistenceException;
import utils.BaseModel;
import utils.SmLogFactory;

/**
 *
 * @author jmoura
 */
public class ProjectDao extends BaseDao implements IProjectDao, Serializable {

    private static final Log log = SmLogFactory.getLog(ProjectDao.class);

    @Override
    public Project get(int id) throws PersistenceException {
        return (Project) super.get(id, SysSql.SelectProjectById);
    }

    @Override
    public List<Project> list() throws PersistenceException {
        log.trace("Loading Projects:");
        List<BaseModel> loadList = super.list(SysSql.SelectProjectList, "id_project");
        //convert back to the desired Class
        //TODO: there must be a better way to cast the entire List.
        List<Project> retList = new ArrayList<>();
        for (BaseModel elem : loadList) {
            retList.add((Project) elem);
        }
        return retList;
    }

    @Override
    protected Project loadColumns(ResultSet rs) throws SQLException, PersistenceException {
        Project project;
        project = new Project();
        project.setId(rs.getInt("id_project"));
        project.setName(rs.getString("nm_project"));
        project.setStart(rs.getDate("dt_start"));
        project.setActive(rs.getString("fl_active").equals("Yes"));
        //load schedule baselines
        List<ScheduleBaseline> list = PersistFactory.getScheduleBaselineDao().list(project);
        for (ScheduleBaseline baseline : list) {
            project.addScheduleBaseline(baseline);
            baseline.setProject(project);
        }
        return project;
    }

    @Override
    public void save(Project project) throws PersistenceException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
